let db=require('../db/dbmanager');
const log4js        = require('../utils/logUtils');
const logger        = log4js.getLogger('model');

/**
 * 拉流代理数据库操作
 */
const TAG = "[Proxy Model]";
let Proxy={
    insert(data){
        return new Promise(function(resolve, reject){
            if(!data.name){
                reject(null);
            }else {
                let param = {
                    $camera_id: data.camera_id,
                    $name: data.name,
                    $mrl: data.mrl,
                    $channel_id: data.channel_id,
                    $record: data.record,
                    $in_use:data.in_use,
                    $server_id:data.server_id
                };
                const sql = "insert into PULL(camera_id,name,mrl,record,channel_id,in_use,server_id)"
                    + " values("
                    + "$camera_id,$name,$mrl,$record,$channel_id,$in_use,$server_id"
                    + ")";
                logger.info(TAG,"insert pull", sql);
                db.sqliteDB.run(sql, param);
                db.sqliteDB.lastRow("PULL", (d) => {
                    resolve(d);
                });
            }
        });
    },
    // 更新
    update(updateData){
        return new Promise(function(resolve){
            let sqldata={
                $camera_id: updateData.camera_id
            };

            let newsql='';
            if(updateData.hasOwnProperty('record')){
                sqldata.$record = updateData.record;
                if(newsql)newsql+=',';
                newsql += "record=$record";
            }
            if(updateData.name){
                sqldata.$name = updateData.name;
                if(newsql)newsql+=',';
                newsql += "name=$name";
            }
            if(updateData.mrl){
                sqldata.$mrl = updateData.mrl;
                if(newsql)newsql+=',';
                newsql += "mrl=$mrl";
            }
            if(updateData.server_id){
                sqldata.$server_id = updateData.server_id;
                if(newsql)newsql+=',';
                newsql += "server_id=$server_id";
            }
            if(updateData.channel_id){
                sqldata.$channel_id = updateData.channel_id;
                if(newsql)newsql+=',';
                newsql += "channel_id=$channel_id";
            }
            if(updateData.hasOwnProperty('in_use')){
                sqldata.$in_use = updateData.in_use;
                if(newsql)newsql+=',';
                newsql += "in_use=$in_use";
            }
            if(newsql){
                const sql = "UPDATE PULL SET "+newsql+" WHERE camera_id=$camera_id";
                db.sqliteDB.run(sql, sqldata);
                resolve(true);
            }else{
                resolve(false);
            }
        });
    },
    // 查询列表
    list: function (page, limit, search) {
        return new Promise(function (resolve) {
            let sql = "SELECT * FROM PULL WHERE 1=1 ";
            if(search){
                sql += ` AND (name LIKE '${search}' OR mrl LIKE '${search}')`;
            }
            const offset = (page - 1) * limit;
            sql += ` ORDER BY camera_id DESC LIMIT ${limit} OFFSET ${offset} `;
            db.sqliteDB.queryData(sql, rows=>{
                resolve(rows);
            });
        });
    },
    remove: function (camera_id) {
        const sqlChannel="DELETE FROM PULL WHERE camera_id = $camera_id";
        let paramChannel = {$camera_id: camera_id};
        db.sqliteDB.run(sqlChannel, paramChannel);
    },
    exist: function (property, value, exceptId) {
        return new Promise(function (resolve, reject) {
            let sql = `SELECT * FROM PULL WHERE ${property} = ${value}`;
            if(exceptId){
                sql += ` AND camera_id != ${exceptId}`;
            }
            db.sqliteDB.queryData(sql,  rows=>{
                resolve(rows.length>0);
            });
        });
    },
    // 取camera_id的最大值
    maxCameraId: function () {
        return new Promise(function (resolve, reject) {
            let sql = "SELECT MAX(camera_id) AS max_camera_id FROM PULL";
            db.sqliteDB.queryData(sql,  rows=>{
                if(rows.length>0) {
                    resolve(rows[0].max_camera_id);
                }else{
                    resolve(0);
                }
            });
        });
    }
}
module.exports=Proxy;
